package com.jiaquyun.jcyx.main.mine

import android.os.Bundle
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import androidx.constraintlayout.motion.widget.MotionLayout
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.entity.*
import com.jiaquyun.jcyx.main.goods.GoodsDetailsActivity
import com.jiaquyun.jcyx.utils.SpanUtils
import com.module.lemlin.image.loadRoundImage
import java.lang.StringBuilder

class AddressListAdapter : BaseQuickAdapter<Address, BaseViewHolder>(R.layout.item_address_list) {

    init {
        addChildClickViewIds(R.id.ivAddressListEdit)
    }

    override fun convert(holder: BaseViewHolder, item: Address) {
        holder.setText(
            R.id.tvAddressListNameAndPhone,
            StringBuilder().append(item.consignee).append("\t\t").append(item.mobile)
        )
            .setText(R.id.tvAddressListDetails, item.full_address)
            .setGone(R.id.tvAddressListDefault, !item.is_default)
    }
}


class CollectGoodsAdapter :
    BaseQuickAdapter<CollectGoods, BaseViewHolder>(R.layout.item_goods_list),
    LoadMoreModule {

    init {
        addChildClickViewIds(R.id.ivGoodsPlus, R.id.ivGoodsMinus)

        setOnItemClickListener { _, _, position ->
            val itemData = data[position]
            val bundle = Bundle().apply {
                putInt(GoodsDetailsActivity.KEY_GOODS_ID, itemData.goods.id)
            }
            ActivityUtils.startActivity(bundle, GoodsDetailsActivity::class.java)
        }
    }

    override fun convert(holder: BaseViewHolder, item: CollectGoods) {
        holder.setText(R.id.tvGoodsName, item.goods.goods_name)
            .setText(
                R.id.tvGoodsPrice,
                SpanUtils.setMoneySymbol(item.goods.guidance_price, item.goods.price)
            )
            .setText(R.id.tvGoodsNum, item.cart_num.toString())
            .setGone(R.id.flGoodsDim, (item.goods.shop_count != 0))
            .getView<ImageView>(R.id.ivGoodsIcon)
            .loadRoundImage(item.goods.image, radius = ConvertUtils.dp2px(5f))

        val motionLayout: MotionLayout = holder.getView(R.id.motionLayout)
        if (item.cart_num == 0) {
            motionLayout.transitionToStart()
        } else {
            motionLayout.transitionToEnd()
        }

    }

    fun setData(position: Int, goodsNum: Int = 0, cartId: Int = 0) {
        this.setData(position, data[position].apply {
            cart_id = if (cartId == 0) cart_id else cartId
            cart_num = goodsNum
        })
    }
}

class PurchaseGoodsAdapter :
    BaseQuickAdapter<PurchaseGoods, BaseViewHolder>(R.layout.item_goods_list),
    LoadMoreModule {

    init {
        setOnItemClickListener { _, _, position ->
            val itemData = data[position]
            if (itemData.goods_id == null)
                return@setOnItemClickListener
            val bundle = Bundle().apply {
                putInt(GoodsDetailsActivity.KEY_GOODS_ID, itemData.goods_id)
            }
            ActivityUtils.startActivity(bundle, GoodsDetailsActivity::class.java)
        }
    }

    override fun convert(holder: BaseViewHolder, item: PurchaseGoods) {
        holder.setText(R.id.tvGoodsName, item.goods_name)
            .setText(
                R.id.tvGoodsPrice,
                SpanUtils.setMoneySymbol(item.guidance_price ?: "0.00", item.price ?: "0.00")
            )
            .setGone(R.id.tvGoodsNum, true)
            .setGone(R.id.ivGoodsPlus, true)
            .setGone(R.id.ivGoodsMinus, true)
            .getView<ImageView>(R.id.ivGoodsIcon)
            .loadRoundImage(item.image ?: "", radius = ConvertUtils.dp2px(5f))

    }
}

class ShopsListAdapter : BaseQuickAdapter<UserShops, BaseViewHolder>(R.layout.item_shops_list) {

    var selectPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    init {
        setOnItemClickListener { _, _, position ->
            selectPosition = position
        }
    }

    override fun convert(holder: BaseViewHolder, item: UserShops) {
        holder.setText(R.id.tvShopsListName, item.shop_name)
            .setText(R.id.tvShopsListAddress, item.address)
            .setText(R.id.tvShopsListDistance, "${item.distance}km")
        holder.getView<CheckBox>(R.id.cbShopsListSelect).apply {
            isEnabled = false
        }.isChecked = selectPosition == holder.layoutPosition
    }
}

class SignListAdapter : BaseQuickAdapter<Sign, BaseViewHolder>(R.layout.item_sign_list) {

    override fun convert(holder: BaseViewHolder, item: Sign) {
        holder.setText(R.id.tvSignListName, item.name)
            .setText(R.id.tvSignListNum, "${item.score ?: 0}积分")
            .getView<ImageView>(R.id.ivSignListSelect).apply {
                if (item.id != null) {
                    visibility = View.VISIBLE
                } else {
                    visibility = View.GONE
                }
            }
    }
}

class ScoreListAdapter : BaseQuickAdapter<UserScore, BaseViewHolder>(R.layout.item_score_list) {

    override fun convert(holder: BaseViewHolder, item: UserScore) {
        holder.setText(R.id.tvScoreListName, item.memo)
            .setText(R.id.tvScoreListTime, item.createtime)
            .setText(R.id.tvScoreListNum, item.score.toString())
    }
}